//
//  ViewController.m
//  WXPay
//
//  Created by ZHY on 2017/9/18.
//  Copyright © 2017年 ZHY. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "WXApi.h"

@interface ViewController ()

@property (nonatomic,strong) PayReq* req;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [payBtn setTitle:@"微信支付" forState:UIControlStateNormal];
    payBtn.frame = CGRectMake(50, 100, [UIScreen mainScreen].bounds.size.width -100, 41);
    payBtn.backgroundColor = [UIColor greenColor];
    [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payBtn];
}



-(void)payClick {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [[AFJSONRequestSerializer alloc] init];
    manager.responseSerializer = [[AFJSONResponseSerializer alloc] init];
    
    NSSet *set = [NSSet setWithObjects:@"application/json",@"text/json",@"text/plain", @"text/html", nil];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:set];
    
    //会过期 不一定能用
    NSString *token = @"f3fe88c4a34a3f2f45f7ebc0245241d88240cadb";
    NSString *eid = @"b7b6faf86dc7468991c4dc9945c35f09";
    NSString *appId = @"2e8e82158a0f4a6fa69c2718a75cbe03";
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:appId forHTTPHeaderField:@"appid"];
    [manager.requestSerializer setValue:eid forHTTPHeaderField:@"eid"];
    self.req = [[PayReq alloc] init];
    NSDictionary *body = @{
                           @"body": @"腾讯充值中心-QQ会员充值165",
                           @"deviceInfo": @"APP",
                           @"productId": @"12",
                           @"spbillCreateIp": @"123.12.12.123",
                           @"totalFree": @(0.01),
                           @"tradeType": @"APP"
                           };
    NSString *url = @"http://api-debug.iqcloud.cc:8082/PaySystem/api/wxpay/doUnifiedOrder";
    
    //此处可省略----目的从后台获取到相应微信支付相应的参数
    //请求后台返回PayReq对应的字段
    [manager POST:url parameters:body progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        PayReq* req             = [[PayReq alloc] init];
        self.req.partnerId           = responseObject[@"data"][@"partnerid"];
        self.req.prepayId            = responseObject[@"data"][@"prepayid"];
        self.req.nonceStr            = responseObject[@"data"][@"noncestr"];
        self.req.timeStamp           = [responseObject[@"data"][@"timestamp"] intValue];
        self.req.package             = responseObject[@"data"][@"package"];
        self.req.sign                = responseObject[@"data"][@"sign"];
        
        //发送请求到微信，等待微信返回onResp
        [WXApi sendReq:self.req];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
   

}






@end
